package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.DataAnalyze;
import com.sysman.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年11月30日 上午11:08:37 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/HospDeptMan")
public class HospDeptController {
	/**
	 * 部门审核界面初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/hospdeptexamine")
	public ModelAndView hospdeptexamine()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/HospDeptMan/HospDeptExamine");
		return model;
	}
	
	/**
	 * 部门审核列表的异步动态加载类，由http获取数据并解析
	 * @param limit
	 * @param offset
	 * @param check
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/HospDeptListAjax")
	public @ResponseBody Map<String,Object> HospDeptListAjax(Integer limit,Integer offset,String check,HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+check+"//"+adminloginid);
		String HospDeptParam = "limit="+limit+"&offset="+offset+"&check="+check+"&adminloginid="+adminloginid;//参数串
		
		List<Map<String,Object>> jsonlist=new ArrayList<>();
		Integer total=0;
		String code="";
		String msg="";
		Map<String, Object> map = new HashMap<String, Object>();  
		if(adminloginid==null){
	        code="-100";
	        msg="请先登录";
		}else{
			try {
				JSONObject HospDeptListjson = HttpRequest.sendPost("listdept", HospDeptParam);
				System.out.println("输出接收到的json数据是："+HospDeptListjson);
				msg = HospDeptListjson.get("msg").toString();
				code = HospDeptListjson.get("code").toString();
				try {
					if(HospDeptListjson.get("data").toString()!="null") {
						JSONObject jsonobjectdata = HospDeptListjson.getJSONObject("data");
						JSONArray jsonarray = jsonobjectdata.getJSONArray("rows");
						total = Integer.parseInt(jsonobjectdata.get("total").toString());
						jsonlist = DataAnalyze.HospDeptJsonToList(jsonarray);
					}
					
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
			        code="-300";
			        msg="数据解析出错";
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
		        code="-200";
		        msg="数据传输错误";
			}
		}
		map.put("msg", msg);
		map.put("rows", jsonlist);
		map.put("code", code);
        map.put("total", total);
		System.out.println("输出这个map："+map);
		return map;
	}
	/**
	 * 获取医院部门
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getHospitalDept")
	public @ResponseBody Map<String,Object> getHospitalDept(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Map<String, Object> ExamineJson = new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<>();
		String code="";
		String msg="";
		if(adminloginid!=null) {
			try {
				JSONObject json = HttpRequest.sendPost("listfirstdept","adminloginid="+adminloginid);
				System.out.println("服务器返回json信息："+json);
		        code = json.get("code").toString();
		        msg = json.get("msg").toString();
		        JSONArray data = json.getJSONArray("data");
				try {
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hospdeptid", job.get("hospdeptid").toString());
						map.put("hospdeptname", job.get("hospdeptname").toString());
						list.add(map);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					msg="数据解析错误";
					code="-300";
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
				msg="数据传输错误";
				code="-200";
			}
		}else {
			msg="请先登录";
			code="-100";
		}
		ExamineJson.put("code", code);
		ExamineJson.put("msg", msg);
		ExamineJson.put("list", list);
		return ExamineJson;
	}
	/**
	 * 部门审核提交ajax
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ExamineDeptSubmit")
	public @ResponseBody Map<String,Object> ExamineDeptSubmit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String hospdeptid = request.getParameter("hospdeptid");
		String hospfatherdeptid = request.getParameter("hospfatherdeptid");
		
		String Param = "adminloginid="+adminloginid+"&hospdeptid="+hospdeptid+"&hospfatherdeptid="+hospfatherdeptid;
		System.out.println("输出param="+Param);
		Map<String,Object> map = new HashMap<String,Object>();
		String code="";
		String msg="";
		if(adminloginid!=null && hospdeptid!=null && hospfatherdeptid!=null) {
			try {
				JSONObject json = HttpRequest.sendPost("checkdept",Param);
				System.out.println("服务器返回json信息："+json);
				code = json.get("code").toString();
			    msg = json.get("msg").toString();
			} catch (Exception e) {
				System.out.println("出错了，错误信息是："+e.getMessage());
				msg="数据传输错误";
				code="-200";
			}
		}else {
			msg="请先登录";
			code="-100";
		}
		map.put("code", code);
	    map.put("msg", msg);
		return map;
	}
}
